﻿using ExcelToEntity.Commands;
using ExcelToEntity.Models;
using Microsoft.Win32;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;

namespace ExcelToEntity.ViewModels
{
    public class VMMainWindow : INotifyPropertyChanged
    {
        public Command? ExportExcelCommand { get; set; } = null;
        public Command? ImportExcelCommand { get; set; } = null;
        public Command? ExportEntityCommand { get; set; } = null;

        private SaveFileDialog? _SaveFileDialog = null;
        private OpenFileDialog? _OpenFileDialog = null;

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// 文件路径
        /// </summary>
        private string FilePath { get; set; } = "";

        #region 导出Excel模板

        public void ExportExcel(object param)
        {
            _SaveFileDialog = new SaveFileDialog();
            _SaveFileDialog.Filter = "Excel文件 (*.xlsx,*.xls)|*.xlsx;*.xls";
            if (_SaveFileDialog.ShowDialog() == true)
            {
                FilePath = _SaveFileDialog.FileName;
            }
            //var values = new List<Dictionary<string, object>>()
            //{
            //    new Dictionary<string, object>(){{ "数据类型(int,string等)","" }, { "数据名称(用户自定义名称)", ""},{ "注释",""} }
            //};
            string fileName = "EntityTemplate.xlsx";
            if (selectedModel.Value == "zh-cn")
            {
                fileName= "ChineseEntityTemplate.xlsx";
            }
            if (selectedModel.Value == "en-us")
            {
                fileName = "EnglishEntityTemplate.xlsx";
            }
            if (!string.IsNullOrEmpty(FilePath))
            {
                var templatePath =Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EntityTemplate", fileName);
                File.Copy(templatePath, FilePath, true);
                //MiniExcel.SaveAs(FilePath, values);
            }
        }

        public bool CanExportExcel(object param)
        {
            return true;
        }

        #endregion

        #region 导入Excel

        private string _nameSpace = "命名空间";
        public string NameSpace
        {
            get { return _nameSpace; }
            set
            {
                _nameSpace = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("NameSpace"));
            }
        }
        private string _className = "类名";
        public string ClassName
        {
            get { return _className; }
            set
            {
                _className = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ClassName"));
            }
        }

        private string _stringBuild = "这里将生成实体类";
        public string StringBuild
        {
            get { return _stringBuild; }
            set
            {
                _stringBuild = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("StringBuild"));
            }
        }


        private void ImportExcel(object param)
        {
            _OpenFileDialog = new OpenFileDialog();
            _OpenFileDialog.Filter = "Excel文件 (*.xlsx,*.xls)|*.xlsx;*.xls";
            if (_OpenFileDialog.ShowDialog() == true)
            {
                FilePath = _OpenFileDialog.FileName;
            }
            if (!string.IsNullOrEmpty(FilePath))
            {
                var rows = MiniExcel.Query<ExcelModel>(FilePath);
                if (selectedModel.Value=="zh-cn")
                {
                    rows=EntityTemplate.ConvertPinYin(rows);
                }
                StringBuild = EntityTemplate.CreateEntityClass(rows, NameSpace, ClassName);
            }
        }

        public bool CanImportExcel(object param)
        {
            return true;
        }

        #endregion

        #region 导出实体类

        public void ExportEntityObject(object param)
        {
            _SaveFileDialog = new SaveFileDialog();
            _SaveFileDialog.Filter = "实体文件 (*.cs,*.cs)|*.cs;*.cs";
            if (_SaveFileDialog.ShowDialog() == true)
            {
                FilePath = _SaveFileDialog.FileName;
            }

            if (!string.IsNullOrEmpty(FilePath))
            {
                //导出实体增加制表符：属性缩进
                var tabsStringBuild = StringBuild.Replace("\r\n\t", "\r\n\t\t");
                File.WriteAllTextAsync(FilePath, tabsStringBuild);
            }
            else
            {
                MessageBox.Show("请输入文件名称");
            }
        }

        public bool CanExportEntityObject(object param)
        {
            return true;
        }

        #endregion

        #region 实体文本框显示
        public Command? CopyEntityCommand { get; set; } = null;
        public Command? ClearEntityCommand { get; set; } = null;

        public void CopyEntity(object param)
        {
            Clipboard.SetText($"{StringBuild}");
        }

        public bool CanCopyEntity(object param)
        {
            return true;
        }

        public void ClearEntity(object param)
        {
            StringBuild = "";
        }

        public bool CanClearEntity(object param)
        {
            return true;
        }

        #endregion

        #region 最小 最大 关闭

        public Command? CloseWndCommand { get; set; } = null;
        public Command? MaxWndCommand { get; set; } = null;
        public Command? MinWndCommand { get; set; } = null;

        private bool CanCloseWnd(object arg)
        {
            return true;
        }

        private void CloseWnd(object obj)
        {
            Environment.Exit(0);
        }

        private bool CanMaxWnd(object arg)
        {
            return true;
        }

        private void MaxWnd(object obj)
        {
            if (MainWindow.MainWnd.WindowState != WindowState.Maximized)
            {
                MainWindow.MainWnd.WindowState = WindowState.Maximized;
            }
            else
            {
                MainWindow.MainWnd.WindowState = WindowState.Normal;
            }
        }

        private bool CanMinWnd(object arg)
        {
            return true;
        }

        private void MinWnd(object obj)
        {
            MainWindow.MainWnd.WindowState = WindowState.Minimized;
        }

        #endregion

        public List<ComboBoxItemModel<Language>> list { get; set; }

        private Language selectedModel;
        public Language SelectedModel
        {
            get { return selectedModel; }
            set
            {

                selectedModel = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Language"));
                Console.WriteLine($"{value.Name} {value.Value}");
            }
        }

        public VMMainWindow()
        {
            ExportExcelCommand = new Command();
            ImportExcelCommand = new Command();
            ExportEntityCommand = new Command();
            CopyEntityCommand = new Command();
            ClearEntityCommand = new Command();

            CloseWndCommand = new Command();
            MaxWndCommand = new Command();
            MinWndCommand = new Command();

            ExportExcelCommand.ExecuteAction = new Action<object>(ExportExcel);
            ExportExcelCommand.CanExecuteAction = new Func<object, bool>(CanExportExcel);

            ImportExcelCommand.ExecuteAction = new Action<object>(ImportExcel);
            ImportExcelCommand.CanExecuteAction = new Func<object, bool>(CanImportExcel);

            ExportEntityCommand.ExecuteAction = new Action<object>(ExportEntityObject);
            ExportEntityCommand.CanExecuteAction = new Func<object, bool>(CanExportEntityObject);

            CopyEntityCommand.ExecuteAction = new Action<object>(CopyEntity);
            CopyEntityCommand.CanExecuteAction = new Func<object, bool>(CanCopyEntity);

            ClearEntityCommand.ExecuteAction = new Action<object>(ClearEntity);
            ClearEntityCommand.CanExecuteAction = new Func<object, bool>(CanClearEntity);

            CloseWndCommand.ExecuteAction = new Action<object>(CloseWnd);
            CloseWndCommand.CanExecuteAction = new Func<object, bool>(CanCloseWnd);

            MaxWndCommand.ExecuteAction = new Action<object>(MaxWnd);
            MaxWndCommand.CanExecuteAction = new Func<object, bool>(CanMaxWnd);

            MinWndCommand.ExecuteAction = new Action<object>(MinWnd);
            MinWndCommand.CanExecuteAction = new Func<object, bool>(CanMinWnd);

            list = new List<ComboBoxItemModel<Language>>
            {
                new ComboBoxItemModel<Language>{Description="中文",SelectedModel=new Language{ Name="中文",Value="zh-cn" },IsEnable=true},
                new ComboBoxItemModel<Language>{Description="英文",SelectedModel=new Language{ Name="英文",Value="en-us" },IsEnable=false},

            };
            SelectedModel = list[0].SelectedModel;

        }
    }
}
